package Control;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ControlContact
 */
public class ControlContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControlContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
//      String result = "";
      boolean result = false;
      String name = request.getParameter("name");
      String email = request.getParameter("email");
      String title = request.getParameter("Title");
      String content = request.getParameter("content");
      String body = "Ý kiến của " + name + "\nEmail: " + email + "\nNội dung: " + content;
      try {
          send("localhost", "admin@myhost.com", "admin@myhost.com", null, title, body);
//          result = "";
          result = true;
      } catch (MessagingException ex) {
          Logger.getLogger(ControlContact.class.getName()).log(Level.SEVERE, null, ex);
      }
      if (result) {
          response.sendRedirect("index.jsp?contact=success");
      } else {
          response.sendRedirect("index.jsp?contact=failure");
      }

//      request.setAttribute("result", result);
//      RequestDispatcher dispathcer = getServletContext().getRequestDispatcher("/index.jsp?contact");
//      dispathcer.forward(request, response);
  }

  public static void send(String smtpServer, String from, String to, String password, String subject, String body) throws MessagingException {

      Properties pro = System.getProperties();
      pro.put("mail.smtp.host", smtpServer);
      pro.put("mail.smtp.port", 25);
      Session session = Session.getInstance(pro, null);
      MimeMessage message = new MimeMessage(session);

      message.setFrom(new InternetAddress(from));
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject(subject, "UTF-8");
      message.setText(body, "UTF-8");
//      message.setContent(body, "text/html");
      Transport tran = session.getTransport("smtp");
      tran.connect(smtpServer, from, null);
      tran.sendMessage(message, message.getAllRecipients());
      tran.close();


  }
		
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
